package com.fagejiang.springbootkafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author 发哥讲Java
 * @version 1.0
 * @date 2021-04-28 11:18
 */
@Component
public class KafkaConsumer {

    @KafkaListener(id = "springboot-kafka-group-consumer", groupId = "springboot-kafka-group", topics = "springboot-kafka-topic")
    public void onMessageConsume(List<ConsumerRecord<String, String>> records) {
        System.out.println("onMessageConsume records.size() = " + records.size());
        for (ConsumerRecord<String, String> record : records) {
            System.out.println(record.value());
            System.out.println(record.value() + "开始休眠 10 秒钟" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
            try {
                TimeUnit.SECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(record.value() + "休眠结束" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        }
    }
}
